cc.Class({
    extends: cc.Component,

    properties: {
        nodeFloor1: {
            default: null,
            type: cc.Node
        },
        nodeFloor2: {
            default: null,
            type: cc.Node
        },
        nodeCloud:{
            default:[],
            type:[cc.Node]
        },
        nodeHome:{
            default:[],
            type:[cc.Node]
        },
        nodeStar:{
            default:null,
            type:cc.Node
        },
    },

    onLoad () {
        this.moveClouds();
        //
        let delayT = gGlobal._random(3, 6);
        this.scheduleOnce(function(){
            this.playLiuxingAni();
        },delayT);
    },

    start () {

    },

    update (dt) {
        if(gGlobal.onGame){
            let boxBird = gGlobal.nodeActor.getBoundingBox();
            //碰到地面游戏结束
            let floor1Box = this.nodeFloor1.getBoundingBox();
            let floor2Box = this.nodeFloor2.getBoundingBox();
            if(cc.Intersection.rectRect(boxBird,floor1Box) || cc.Intersection.rectRect(boxBird,floor2Box)){
                // this.gameOver();
            }
        }

        //移动地板
        this.nodeFloor1.x -= this.moveSpace;
        this.nodeFloor2.x -= this.moveSpace;
        if(this.nodeFloor1.x <= -720){
            this.nodeFloor1.x = this.nodeFloor2.x + 800-1
        }
        if(this.nodeFloor2.x <= -720){
            this.nodeFloor2.x = this.nodeFloor1.x + 800-1;
        }

        //移动房子
        for(let i=0;i<this.nodeHome.length;i++){
            let home = this.nodeHome[i];
            home.x -= 3;
            if(home.x <= -649){
                let id = ((i+1) == this.nodeHome.length ) ? 0 : (i+1);
                let home2 = this.nodeHome[id];
                let rd = gGlobal._random(30, 250);
                home.x = home2.x + 640 + rd;
            }
        }
    },

    //移动云朵
    moveClouds:function(id){
        for(let i=0;i<this.nodeCloud.length;i++){
            let cloud1 = this.nodeCloud[i];
            let x = cloud1.position.x;
            let y = cloud1.position.y;
            let time = ((x+420)/840)*50;
            cc.log("moveClouds--i:"+time);
            let move1 = cc.moveTo(time,cc.v2(-420, y));
            var that = this;
            cloud1.runAction(cc.sequence(move1,cc.callFunc(function(target, id){
                cc.log("moveClouds--callFunc--id:"+id);
                that.moveCloudById(id);
            },this,i)));
        }
    },

    moveCloudById:function(id){
        let cloud = this.nodeCloud[id];
        let time = 50;
        let y = cloud.position.y;
        cloud.setPosition(cc.p(420,y));
        let move1 = cc.moveTo(time,cc.v2(-420, y));
        var that = this;
        cloud.runAction(cc.sequence(move1,cc.callFunc(function(){
            that.moveCloudById(id);
        },this,1)));
    },

    // 流星动画
    playLiuxingAni:function() {
        cc.log("---流星动画:",this.nodeStar);
        if (this.nodeStar) {
            this.nodeStar.active = true;
            var ani = this.nodeStar.getComponent(cc.Animation);
            // cc.log("---ani:",ani);
            if (ani) {
                ani.play();

                ani.on('finished', function () {
                    // cc.log("---finished---");
                    this.nodeStar.active = false;
                }, this);
            }
        }
        let delayT = gGlobal._random(3, 8);
        this.scheduleOnce(function(){
            this.playLiuxingAni();
        },delayT);
    },
});
